package cn.community.care.module.system.service.shoppingcart;

import cn.community.care.framework.common.exception.ServerException;
import cn.community.care.framework.common.exception.ServiceException;
import cn.community.care.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.community.care.framework.security.core.LoginUser;
import cn.community.care.framework.security.core.util.SecurityFrameworkUtils;
import cn.community.care.module.system.controller.admin.orders.vo.OrdersSaveReqVO;
import cn.community.care.module.system.controller.admin.shoppingcart.vo.ShoppingCartPageReqVO;
import cn.community.care.module.system.controller.admin.shoppingcart.vo.ShoppingCartSaveReqVO;
import cn.community.care.module.system.dal.dataobject.hotproducts.HotProductsDO;
import cn.community.care.module.system.dal.dataobject.orders.OrdersDO;
import cn.community.care.module.system.dal.dataobject.shoppingcart.ShoppingCartDO;
import cn.community.care.module.system.dal.mysql.hotproducts.HotProductsMapper;
import cn.community.care.module.system.dal.mysql.shoppingcart.ShoppingCartMapper;
import cn.community.care.module.system.service.orders.OrdersService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.util.object.BeanUtils;

import javax.annotation.Resource;

import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 购物车 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private OrdersService ordersService;

    @Resource
    private HotProductsMapper hotProductsMapper;

    private final static String ORDER_NUMBER = "num";


    @Transactional
    @Override
    public Long createShoppingCart(ShoppingCartSaveReqVO createReqVO) {
        List<ShoppingCartDO> shoppingCartDOS = shoppingCartMapper.selectList(new LambdaQueryWrapperX<ShoppingCartDO>().eq(ShoppingCartDO::getProductId, createReqVO.getProductId())
                .ne(ShoppingCartDO::getPaymentStatus, "2"));
        if (!shoppingCartDOS.isEmpty()){
            throw new ServiceException(400, "商品已加入购物车");
        }
        // 插入
        ShoppingCartDO shoppingCart = BeanUtils.toBean(createReqVO, ShoppingCartDO.class);
        shoppingCart.setPaymentStatus("1");
        shoppingCart.setQuantity(1);
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        shoppingCart.setUserId(userId);
        shoppingCartMapper.insert(shoppingCart);
        //插入成功之后要扣减库存
        HotProductsDO hotProductsDO = hotProductsMapper.selectById(createReqVO.getProductId());
        String currentNumer = hotProductsDO.getNumber();
        //买的数量
        Integer quantity = createReqVO.getQuantity();
        //当前库存
        Integer number = Integer.valueOf(currentNumer);
        if (number<quantity){
            throw new ServiceException(400, "商品库存不足");
        }
        hotProductsDO.setNumber(String.valueOf(number-quantity));
        hotProductsMapper.updateById(hotProductsDO);
        // 返回
        return shoppingCart.getId();
    }

    @Override
    public void updateShoppingCart(ShoppingCartSaveReqVO updateReqVO) {
        // 校验存在
        validateShoppingCartExists(updateReqVO.getId());
        // 更新
        ShoppingCartDO updateObj = BeanUtils.toBean(updateReqVO, ShoppingCartDO.class);
        int update = shoppingCartMapper.updateById(updateObj);
    }

    @Transactional
    @Override
    public void subShoppingCart(ShoppingCartSaveReqVO updateReqVO) {
        // 校验存在
        validateShoppingCartExists(updateReqVO.getId());
        // 更新
        ShoppingCartDO updateObj = BeanUtils.toBean(updateReqVO, ShoppingCartDO.class);
        int update = shoppingCartMapper.updateById(updateObj);
        if (update>0){
            OrdersSaveReqVO ordersDO = BeanUtils.toBean(updateObj, OrdersSaveReqVO.class);
            ordersDO.setId(null);
            ordersDO.setCartId(updateObj.getId());
            ordersDO.setProductCartCode(ORDER_NUMBER+ updateObj.getId()+ordersDO.getProductId());
            ordersService.createOrders(ordersDO);
        }
    }

    @Override
    public void deleteShoppingCart(Long id) {
        // 校验存在
        validateShoppingCartExists(id);
        // 删除
        shoppingCartMapper.deleteById(id);
    }

    private void validateShoppingCartExists(Long id) {
        if (shoppingCartMapper.selectById(id) == null) {
//            throw exception(SHOPPING_CART_NOT_EXISTS);
        }
    }

    @Override
    public ShoppingCartDO getShoppingCart(Integer id) {
        return shoppingCartMapper.selectById(id);
    }

    @Override
    public PageResult<ShoppingCartDO> getShoppingCartPage(ShoppingCartPageReqVO pageReqVO) {
        return shoppingCartMapper.selectPage(pageReqVO);
    }

}